package com.generic.action.member;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.interceptor.validation.SkipValidation;
import org.hibernate.HibernateException;

import com.generic.dao.impl.FeedDao;
import com.generic.dao.impl.ImageDao;
import com.generic.dao.impl.MemberDao;
import com.generic.ds.HibernateSession;
import com.generic.module.impl.Feed;
import com.generic.module.impl.Image;
import com.generic.module.impl.Member;
import com.generic.util.Constant;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@Namespace("/member")
@ParentPackage("member")
@InterceptorRefs({
	@InterceptorRef("jsonValidationWorkflowStack"),
	@InterceptorRef("authorizationStack")	
})
public class Profile extends ActionSupport {

	private static final long serialVersionUID = -4516850784731734347L;
	
	private Member member;
	private Image image;
	private List<Feed> feeds;
	private String message;
	private Long id;
	private String firstName = null;
	private String lastName = null;
	private String email = null;
	private String gender = null;
	private Date birthday = null;
	
	public List<Feed> getFeeds() {
		return feeds;
	}

	public void setFeeds(List<Feed> feeds) {
		this.feeds = feeds;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public Image getImage() {
		return image;
	}

	public void setImage(Image image) {
		this.image = image;
	}

	public Member getMember() {
		return member;
	}

	public void setMember(Member member) {
		this.member = member;
	}

	public String getFirstName() {
		return firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public String getEmail() {
		return email;
	}

	public String getGender() {
		return gender;
	}

	public Date getBirthday() {
		return birthday;
	}
		
	public Long getId() {
		return id;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
		
	public void setId(Long id) {
		this.id = id;
	}

	@SkipValidation
	@Action(value = "/profile", results = {
		@Result(name = "success", location = "member/profile.jsp") })
	public String execute() {
		Map<String, Object> session = ActionContext.getContext().getSession();
		member = (Member) session.get(Constant.SESSION_MEMBER);
		Long memberId = member.getId();
		
		HibernateSession hs = new HibernateSession();
		try {
			ImageDao imageDao = new ImageDao(hs);			
			image = (Image)imageDao.find(memberId);
			FeedDao feedDao = new FeedDao(hs);
			feeds = feedDao.getList(memberId);
		} catch (Exception e) {
			hs.rollbackTransaction(e);
		} finally {
			hs.closeSession();
		}
		return SUCCESS;
	}

	@Action(value = "/feed-add", results = {
			@Result(name = "success", location = "ajax-echo.jsp") })
		public String addFeed() {
			Map<String, Object> session = ActionContext.getContext().getSession();
			member = (Member) session.get(Constant.SESSION_MEMBER);
			Long memberId = member.getId();
			
			HibernateSession hs = new HibernateSession();
			try {
				if(message != null) {
					hs.beginTransaction();
					
					Feed feed = new Feed();
					feed.setForeignId(memberId);
					feed.setType(Constant.MSG_FEED);
					feed.setMessage(message);
					
					FeedDao feedDao = new FeedDao(hs);
					feedDao.insertOrUpdate(feed);
					
					hs.commitTransaction();
					addActionMessage(message);
				}
			} catch (Exception e) {
				hs.rollbackTransaction(e);
			} finally {
				hs.closeSession();
			}
			return SUCCESS;
		}
	
	@Action(value = "/profile-info")
	public String profileInfo() {
//		Map<String, Object> session = ActionContext.getContext().getSession();
//		Member member = (Member) session.get(Constant.SESSION_MEMBER);
//		Long memberId = member.getId();
//		Long memberId = 1l;
		
		HibernateSession hs = new HibernateSession();
		
		MemberDao dao = new MemberDao(hs);
		Member member = (Member)dao.find(id);
		setEmail(member.getEmail());
		setFirstName(member.getFirstName());
		setLastName(member.getLastName());
		setGender(member.getGender());
		setBirthday(member.getBirthday());
		
		return SUCCESS;
	}
	
	@Action(value = "/profile-info-edit")
	public String profileInfoEdit() {
		Map<String, Object> session = ActionContext.getContext().getSession();
		Member member = (Member) session.get(Constant.SESSION_MEMBER);
//		Long memberId = member.getId();
		
//		HibernateSession hs = new HibernateSession();
//		Long memberId = 1l;
//		MemberDao dao = new MemberDao(hs);
//		Member member = (Member)dao.find(memberId);
		setEmail(member.getEmail());
		setFirstName(member.getFirstName());
		setLastName(member.getLastName());
		setGender(member.getGender());
		setBirthday(member.getBirthday());
		
		return SUCCESS;
	}
	
	@Action(value = "/profile-info-save", results = { 
			@Result(name = "input", location = "ajax-msg.jsp"), 
			@Result(name = "success", location = "profile-info.jsp") })
		public String profileInfoSave() {
			Map<String, Object> session = ActionContext.getContext().getSession();
			Member member = (Member) session.get(Constant.SESSION_MEMBER);
		
			HibernateSession hs = new HibernateSession();
			try {
				hs.beginTransaction();
//				Long memberId = 1l;
				MemberDao dao = new MemberDao(hs);
//				Member member = (Member)dao.find(memberId);
				member.setEmail(email);
				member.setFirstName(firstName);
				member.setLastName(lastName);
				member.setGender(gender);
				member.setBirthday(birthday);
				dao.insertOrUpdate(member);
				hs.commitTransaction();
				addActionMessage(getText("success"));
			} catch (HibernateException e) {
				hs.rollbackTransaction(e);
			} finally {
				hs.closeSession();
			}
			
			return SUCCESS;
		}
}
